//
//  SXNewFeatureController.swift
//  微博09项目
//
//  Created by mac on 15/9/7.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit

private let SXNewFeatureCellID = "SXNewFeatureCellID"
/// 新特性图片数量
private let SXNewFeatureCount = 4


class SXNewFeatureController: UICollectionViewController {
    //构造函数要方便外部的代码调用，不要指定构造属性
    init(){
        super.init(collectionViewLayout:UICollectionViewFlowLayout())
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        self.collectionView!.registerClass(SXNewFeatureCell.self, forCellWithReuseIdentifier: SXNewFeatureCellID)
        prepareLayout()
    }
    
    
    private func prepareLayout(){
        let layout = collectionView!.collectionViewLayout as! UICollectionViewFlowLayout
        layout.itemSize = view.bounds.size
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.scrollDirection =  UICollectionViewScrollDirection.Horizontal
       
        collectionView?.bounces = false
        collectionView?.showsHorizontalScrollIndicator = false
        collectionView?.pagingEnabled = true
    }
    
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return SXNewFeatureCount
    }
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(SXNewFeatureCellID
            , forIndexPath: indexPath) as! SXNewFeatureCell
       // cell.backgroundColor = UIColor.blueColor()
        cell.imageIndex = indexPath.item
        return cell
    }
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        let path = collectionView.indexPathsForVisibleItems().last!
        
        if path.item == SXNewFeatureCount - 1{
            let cell = collectionView.cellForItemAtIndexPath(path) as! SXNewFeatureCell
            
            cell.showStartButton()
        }

    }  }

class SXNewFeatureCell:UICollectionViewCell {
    
    var imageIndex: Int = 0{
        didSet{
            iconView.image = UIImage(named: "new_feature_\(imageIndex + 1)")
            startButton.hidden = true
        }
    }
    //设置按钮自动布局
    private func showStartButton(){
        startButton.hidden = false
        //按钮的缩放
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        /// - parameter <Tduration:             <#T##duration: NSTimeInterval##NSTimeInterval#> 延续时间
        /// - parameter delay:                  延迟时间
        /// - parameter usingSpringWithDamping: 弹性系数，在0 - 1之间，越小弹性越大
        /// - parameter initialSpringVelocity:  弹性速度
        /// - parameter options:                数组直接天[]
       

        UIView.animateWithDuration(1.2, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10, options: [], animations: { () -> Void in
            self.startButton.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
                
        }
        //startButton
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setupUI()
    }
    private func setupUI(){
       
        addSubview(iconView)
        addSubview(startButton)
        iconView.frame = bounds
        
        startButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: startButton, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: startButton, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -160))
    }
    private lazy var iconView = UIImageView()
    
    private lazy var startButton: UIButton = {
        let button = UIButton()
        
        button.setTitle("开始体验", forState:UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        button.backgroundColor = UIColor.blueColor()
        button.sizeToFit()
        
        button.addTarget(self, action: "clickStartButton", forControlEvents: UIControlEvents.TouchUpInside)
            
        return button
    }()
    @objc func clickStartButton(){
      //  print(__FUNCTION__)
        //发送通知
        NSNotificationCenter.defaultCenter().postNotificationName(SXSwitchRootViewControllerNotification, object: nil)
    }
}

